package com.veal98.stack_queue;

import java.util.Stack;

public class EvaluateReversePolishNotation_150 {

    public int evalRPN(String[] tokens) {
        if (tokens == null || tokens.length == 0) {
            return 0;
        }

        Stack<Integer> stack = new Stack<>();

        for (String s : tokens) {
            if (s.equals("+") || s.equals("-") || s.equals("*") || s.equals("/")) {
                int op1 = stack.pop();
                int op2 = stack.pop();
                int res = 0;
                switch (s) {
                    case "+":
                        res = op1 + op2;
                        break;
                    case "-":
                        res = op1 - op2;
                        break;
                    case "*":
                        res = op1 * op2;
                        break;
                    case "/":
                        res = op1 / op2;
                        break;
                }
                stack.push(res);
            } else {
                stack.push(new Integer(s));
            }
        }

        // 栈中的最后一个元素就是最终结果
        return stack.pop();
    }

}